var teacherService = new TeacherService();
var teacherList = null;

var curLoadDataCount = 0;
var maxLoadDataCount = 1;

$(function () {
    loadInfo();
})

function loadInfo() {
    // 设置教师数据
    teacherService.getList(taskId, function (list) {
        teacherList = list;
        loadFinished();
    })
}

function loadFinished() {
    curLoadDataCount++;
    if (curLoadDataCount >= maxLoadDataCount) {
        renderInfo();
    }
}

function renderInfo() {
    renderTeacherInfo();
}

function renderTeacherInfo() {
    if (teacherList == null) {
        return;
    }
    var col = 5;
    var htmlArr = [];
    htmlArr.push("<div style='font-weight: bold;margin-bottom: 10px'>老师信息</div>")
    htmlArr.push("<table class='info-edit-teacher-table'>")
    var j = 0;
    for (var i = 0; i < teacherList.length; i++) {
        var teacher = teacherList[i]
        j = i + 1;

        if (i % col == 0) {
            htmlArr.push("<tr>");
        }

        htmlArr.push("<td>" + j + "</td>" +
            "<td><input type='text' id='t_" + teacher.id + "' value='" + teacher.name + "' onkeyup='change(\"" + teacher.id + "\");'> " +
            "<button id='btn_" + teacher.id + "' class='btn btn-primary btn-xs ' onclick='updateTeacher(\"" + teacher.id + "\")' disabled='disabled'>更新</button></td>"
        )

        if (j % col == 0) {
            htmlArr.push("</tr>");
        }
    }

    if (j % col != 0)  htmlArr.push("</tr>");

    htmlArr.push("</table>")

    $("#info_edit").html(htmlArr.join(""));
}

/**
 *
 * @param teacherId
 * @returns {boolean}
 */
function change(teacherId) {
    var name = $("#t_" + teacherId).val();
    var name = name.trim();

    for (var i = 0; i < teacherList.length; i++) {
        var teacher = teacherList[i]
        if (teacher.id != teacherId) {
            continue;
        }

        if (teacher.name.trim() == name) {
            enabledUpdateButton(teacherId, false);
            break;
        }

        enabledUpdateButton(teacherId, true);
    }
}

function updateTeacher(teacherId) {
    var name = $("#t_" + teacherId).val();
    var name = name.trim();
    if (name == "") {
        alert("名称不能为空");
        return;
    }

    if (!checkTeacherName(teacherId, name)) {
        alert("有相同名字的老师，无法更新");
        return;
    }

    teacherService.updateName(taskId, teacherId, name, function (ret) {
        if (ret == "1") {
            updateSuccess(teacherId, name);
        } else {
            alert("更新失败,请重试！");
        }
    })
}

function checkTeacherName(teacherId, name) {
    for (var i = 0; i < teacherList.length; i++) {
        var teacher = teacherList[i]
        if (teacher.id == teacherId) {
            continue;
        }

        if (teacher.name.trim() == name) {
            return false;
        }
    }

    return true;
}

function updateSuccess(teacherId, name) {
    alert("更新完成");

    //
    enabledUpdateButton(teacherId, false);

    // 更新teacherList
    for (var i = 0; i < teacherList.length; i++) {
        var teacher = teacherList[i]
        if (teacher.id == teacherId) {
            teacher.name = name;
            break;
        }
    }

    // 重新加载基础信息
    var baseInfoService = new BaseInfo();
    baseInfoService.getBaseInfo(taskId, function (ret) {
        if (ret != null) {
            uploadTeacherSuccess(ret);
        }
    });
}

function enabledUpdateButton(teacherId, enabled) {
    var btn = $("#btn_"+teacherId);
    if(!enabled){
        btn.attr("disabled","disabled");
    }else {
        btn.removeAttr("disabled");
    }
}